package com.cn.ServiceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cn.Domain.OrderSetting;
import com.cn.Mapper.OrderSettingMapper;
import com.cn.Service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //检查此数据（日期）是否存在 存在更新，不存在新增
                long countByOrderDate = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderdate());
                if (countByOrderDate>0){
                    orderSettingMapper.updateByPrimaryKeySelective(orderSetting);
                }else {
                    orderSettingMapper.insertSelective(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getAllOrderSettingByMonth(String date) {
        Map map = new HashMap();
        String st = date.substring(5);
        if (st.equals("2")||st.equals("4")||st.equals("6")||st.equals("9")||st.equals("11")){
            String dateBegin = date + "-1";//2019-3-1
            String dateEnd = date + "-30";//2019-3-31
            map.put("dateBegin",dateBegin);
            map.put("dateEnd",dateEnd);
        }else {
            String dateBegin = date + "-1";//2019-3-1
            String dateEnd = date + "-31";//2019-3-31
            map.put("dateBegin",dateBegin);
            map.put("dateEnd",dateEnd);
        }


        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderdate().getDate());//获得日期（几号）
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderdate = orderSetting.getOrderdate();
        //检查此数据（日期）是否存在 存在更新，不存在新增
        long countByOrderDate = orderSettingMapper.findCountByOrderDate(orderdate);
        if (countByOrderDate>0){
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            orderSettingMapper.add(orderSetting);
        }
    }
}
